package com.example.day12_7_12_4.base;


import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

//T 指定的是子条目的数据
public abstract class BaseRlvAdapter<T> extends RecyclerView.Adapter<BaseRlvAdapter.BaseViewHolder> {
    private Context mContext;
    private List<T> mList;

    public BaseRlvAdapter(Context context, List<T> list) {
        mContext = context;
        mList = list;
    }

    /**
     * onCreateViewHolder 只有设置适配器和刷新的时候调用
     *
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(getLayout(), parent, false);
        return new BaseViewHolder(inflate);
    }

    protected abstract int getLayout();

    //没显示一个子条目都会被调用1次
    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        T t = mList.get(position);
        bindData(holder, t);
    }

    public void setData(List<T> list){
        Log.i("TAG", "setData: baseAdapter");
        mList.addAll(list);
        notifyDataSetChanged();
    }

    protected abstract void bindData(BaseViewHolder holder, T t);

    //加载更多
    public void addData(List<T> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    //下拉刷新,旧的数据需要清除
    public void updateData(List<T> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {

        //Android开发的时候一般不用HashMap
        //为什么不用,因为HashMap消耗内存比较多
        //HashMap<Integer, View> mMap = new HashMap<>();

        //和hashMap一样,都是存储键值对的,但是它更轻量级,
       /* ArrayMap;
        SparseArray*/
        SparseArray<View> mMap = new SparseArray<>();

        public BaseViewHolder(View itemView) {
            super(itemView);
            //父类不值道最终子类子条目的布局究竟有哪些id
            //itemView.findViewById(R.id.xx);
        }

        //用来findviewBYid的
        //findViewById 查找到的控件保存到容器中,下次再调用这个方法的时候,先从容器中找,找不到再findViewByid
        public View findView(@IdRes int id) {
            View view = mMap.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mMap.put(id,view);
            }

            return view;
        }

        public void setText(@IdRes int id,String text){
            //有可能有人传过来的id不是TextView的id,这样强转的时候就崩了
            try {
                TextView view = (TextView) findView(id);
                view.setText(text);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
